use crate::storage::{Storage, TodoItem};
use crate::ExecuteCommand;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;

#[derive(Parser, Debug)]
pub struct Id {
    /// 要查看的任务的 ID
    pub id: u32,
}

impl ExecuteCommand for Id {
    fn execute_command(&self, storage: &impl Storage) -> Result<()> {
        if let Some(todo) = storage.find_by_id(self.id)? {
            print_todo(todo);
        }
        Ok(())
    }
}

fn print_todo(todo: TodoItem) {
    println!("ID: {}\nContent: {}\nStatus: {}\nDate: {}\nCompleted At: {}\nPriority: {}\nCreated At: {}",
             todo.id.to_string().cyan(),
             todo.content.to_string().white(),
             match todo.status {
                 1 => "Completed".green(),
                 _ => "Pending".yellow(),
             },
             todo.date.format("%Y-%m-%d").to_string().yellow(),
             todo.completed_at.map_or_else(|| "".to_string(), |t| t.format("%Y-%m-%d %H:%M:%S").to_string()),
             match todo.priority {
                 1 => "高".red(),
                 _ => "低".white(),
             },
             todo.created_at.map_or_else(|| "".to_string(), |t| t.format("%Y-%m-%d %H:%M:%S").to_string()));
}

impl Id {
    pub fn new(id: u32) -> Self {
        Self { id }
    }
}

impl From<u32> for Id {
    fn from(id: u32) -> Self {
        Self::new(id)
    }
}